global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A23_cipc_examples.log, replace name(tabl)

capture noi { 

    ***************************************************
    * High and low prevalence c/ipc code examples
    ***************************************************

    texdoc init ${tab_dir}/appendix/Table_A23_cipc_examples.tex, replace
    texdoc stlog, nolog

    * get statistics

    import delim ${classification_dir}/V6/ipc6XX_tf.csv, varnames(1) clear
    foreach v of varlist * {
    label var `v' ""
    }

    * Identify machinery codes 
    gen machinery_field = techn_sector == "Mechanical engineering" & (techn_field == "Handling" ///
        | techn_field == "Machine tools" | techn_field == "Other special machines" ///
        | techn_field == "Textile and paper machines")
    rename ipc6 cipc6
    gen cipc1 = substr(cipc6, 1, 1)
    gen cipc3 = substr(cipc6, 1, 3)
    gen cipc4 = substr(cipc6, 1, 4)
    replace machinery_field = 0 if cipc1 == "Y"
    replace machinery_field = 0 if cipc3 == "F41" | cipc3 == "F42"
    replace machinery_field = 1 if cipc4 == "B42C" | cipc4 == "B07C"
    replace machinery_field = 1 if cipc6 == "G05B19" | cipc6 == "G05B2219" | cipc6 == "B62D65"
    replace techn_field = "non-machinery" if machinery_field == 0
    replace techn_field = "non-classified" if cipc1 == "Y"

    * Restrict to machinery codes with at least 100 patents and define relative rank
    keep if machinery_field == 1 & total >= 100
    ren (total share_anyclassification) (patents share_any)
    keep cipc6 patents share_* 
    rename share_* *
    label drop _all
    gsort -any
    gen rank = _n
    ren patents nr_patents


    * Pick some high prevalence code examples
    gen high_prev = inlist(cipc6, "B25J5", "B25J9", "B23Q15", "A01J7", "G05B19", "B65G1")
    sort rank
    estpost tabstat nr_patents any rank robot automat cnc labor if high_prev == 1, by(cipc6)

    forval i = 1/6 { 
        foreach comp in any robot automat cnc labor {
            sleep 1000
            texdoc local h`i'`comp' : display %9.2f e(`comp')[1,`i']
        }
        foreach comp in nr_patents rank {
            sleep 1000
            texdoc local h`i'`comp' : display %9.0f e(`comp')[1,`i']
        }
    }
    list cipc6 if high_prev == 1

    texdoc local B23Q15_descr "\makecell{Automatic control or regulation of feed movement,\\ cutting velocity or position of tool or work}"
    sleep 1000
    texdoc local B25J5_descr "Manipulators mounted on wheels or on carriages"
    sleep 1000
    texdoc local B25J9_descr "Programme-controlled manipulators"
    sleep 1000
    texdoc local B65G1_descr "\begin{tabular}[t]{@{}l@{}}Storing articles, individually or in orderly \\arrangement, in warehouses or magazines\end{tabular}"
    sleep 1000
    texdoc local G05B19_descr "Programme-control systems"
    sleep 1000
    texdoc local A01J7_descr "Accessories for milking machines or devices"

    * Pick some low prevalence code examples
    gen low_prev = inlist(cipc6, "B23P6", "A01B63", "B66D3")
    eststo clear 
    sort rank
    estpost tabstat nr_patents any rank robot automat cnc labor if low_prev == 1, by(cipc6)

    forval i = 1/3 { 
        foreach comp in any robot automat cnc labor {
            sleep 2000
            texdoc local l`i'`comp' : display %9.2f e(`comp')[1,`i']
        }
        foreach comp in nr_patents rank {
            sleep 2000
            texdoc local l`i'`comp' : display %9.0f e(`comp')[1,`i']
        }
    }
    list cipc6 if low_prev == 1
    texdoc local A01B63_descr "\begin{tabular}[t]{@{}l@{}}Lifting or adjusting devices or arrangements for \\ agricultural machines or implements\end{tabular}"
    sleep 1000
    texdoc local B23P6_descr "Restoring or reconditioning objects"
    sleep 1000
    texdoc local B66D3_descr "Portable or mobile lifting or hauling appliances"
    sleep 1000

    * output table (via texdoc)
    texdoc local notes "This table reports the prevalence of automation keywords for examples of 6-digit C/IPC codes. 'Any' is the share of patents with any of the keywords. 'Rank' is the rank of the code among 986 6-digit C/IPC codes in machinery with at least 100 patents. 'Robot' , 'Automat*', 'CNC' and 'labor' are the shares of patents with at least one keyword from these categories."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{\hspace*{0.3em}}llccccccc@{}}
    \toprule
    {\hspace*{-0.3em}Code} & Description & \# Patents & Any & Rank & Robot & Automat* & CNC & Labor \\
    \midrule
    \noalign{\smallskip}
    \multicolumn{2}{@{}l}{High Prevalence Codes\hspace*{-0.3em}}\\  
    \noalign{\smallskip}
    B25J5  &  `B25J5_descr' & `h3nr_patents' & `h3any' & `h3rank' & `h3robot' & `h3automat' & `h3cnc' & `h3labor' \\
    B25J9  & `B25J9_descr' & `h4nr_patents' & `h4any' &  `h4rank' & `h4robot' & `h4automat' & `h4automat' & `h4labor' \\
    B23Q15  & `B23Q15_descr' & `h2nr_patents' & `h2any' &  `h2rank' & `h2robot' & `h2automat' & `h2automat' & `h2labor' \\
    A01J7  & `A01J7_descr' & `h1nr_patents' & `h1any' &  `h1rank' & `h1robot' & `h1automat' & `h1automat' & `h1labor' \\
    G05B19  & `G05B19_descr' & `h6nr_patents' & `h6any' &  `h6rank' & `h6robot' & `h6automat' & `h6automat' & `h6labor' \\
    B65G1  & `B65G1_descr' & `h5nr_patents' & `h5any' &  `h5rank' & `h5robot' & `h5automat' & `h5automat' & `h5labor' \\     

    \noalign{\smallskip}
    \multicolumn{2}{@{}l}{Low Prevalence Codes}\\
    \noalign{\smallskip}        

    B23P6 & `B23P6_descr' &  `l2nr_patents' & `l2any' & `l2rank' & `l2robot' &  `l2automat' &  `l2cnc' & `l2labor' \\
    A01B63 & `A01B63_descr' &  `l1nr_patents' & `l1any' & `l1rank' & `l1robot' &  `l1automat' &  `l1cnc' & `l1labor' \\         
    B66D3 & `B66D3_descr' &  `l3nr_patents' & `l3any' & `l3rank' & `l3robot' &  `l3automat' &  `l3cnc' & `l3labor' \\         
    \bottomrule
    \end{tabular}
    tex*/

    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}